Ext.namespace('pages.attachment.attMgmt');

pages.attachment.attMgmt.AttrMgmt = function(config) {
	Ext.apply(this, config);
	this.operType,this.record;
	if(config && config.operType){
		this.operType = config.operType;
	}
	if(config && config.record){
		this.record = config.record;
	}
	this.initUIComponents();
	pages.attachment.attMgmt.AttrMgmt.superclass.constructor.call(this);
};

Ext.extend(pages.attachment.attMgmt.AttrMgmt, Ext.Window, {
	
	initUIComponents : function() {
		
		this.recordAll = new Ext.data.Record({	
			comName : '全部',
			comCode : '-1'
		});
		
		this.menuRoleStore = new Ext.data.Store({
			proxy : new Ext.data.HttpProxy({
				url : ExtJ.getActionPath("menu/queryRoleList.do")
			}),
			reader : new Ext.data.JsonReader({
				root : 'data',
				totalProperty : 'total',
				id : 'ID'
			}, [{
				name : 'comName',
				mapping : 'comName'
			}, {
				name : 'comCode',
				mapping : 'comCode'
			}])
		});
		this.menuRoleStore.load({
			callback : function(store, records) {
				this.menuRoleStore.insert(0,this.recordAll);
				if('view'==this.operType){
					Ext.getCmp('SHARE_ROLE').onViewInit(this.record.get("SHARE_ROLE_IDS"));
				}
			}.createDelegate(this)
		});
		var menuRoleCombo = new Ext.form.MultiComboBox({
			fieldLabel : '共享部门',
			name : 'SHARE_ROLE',
			id : 'SHARE_ROLE',
			anchor : '96%',
			store : this.menuRoleStore,
			valueField : 'comCode',
			displayField : 'comName',
			hiddenName : 'SHARE_ROLE_VALUE',
			triggerAction : 'all',
			mode : 'local',
			editable : false,
			selectOnFocus : true,
			listeners : {
				'select' : function(){
					var shareRoleIds = Ext.getCmp('SHARE_ROLE').getValue();
					var ids = shareRoleIds.split(",");
					for(var i=0;i<ids.length;i++){
						if("-1"==ids[i]){
							Ext.getCmp('SHARE_ROLE').checkAll();
						}
					}
				}.createDelegate(this)
			}
		});
		this.form = new Ext.form.FormPanel({
				layout : 'column',
				region : 'north',
				height : 180,
				labelWidth : 65,
				labelAlign : 'right',
				autoScroll : false,
				border : false,
				frame : false,
				tbar : [{
					text : '保存',
					hidden :  false,
					handler : this.onSave.createDelegate(this)
				},{
					text : '关闭',
					handler : this.onCancel.createDelegate(this)
				}],
				items : [{
							columnWidth : 1,
							height : 10,
							border : false
						},{
							columnWidth : .5,
							layout : 'form',
							border : false,
							items : [new Ext.form.Hidden({
								id : 'UPLOAD_OPER_USER_ID',
								name : 'UPLOAD_OPER_USER_ID',
								value : 'add' == this.operType ?gyh:''
							}),{
								fieldLabel : '操作者',
								id : 'UPLOAD_OPER_USER_NAME',
								name : 'UPLOAD_OPER_USER_NAME',
								value : 'add' == this.operType ?username:'',
								anchor : '96%',
								readOnly : true,
								xtype : 'textfield'
							}]
						},
						{
							columnWidth : .5,
							layout : 'form',
							border : false,
							items : [{
								fieldLabel : '操作时间',
								id : 'UPLOAD_DATE',
								name : 'UPLOAD_DATE',
								anchor : '96%',
								format : 'Y-m-d',
								xtype : 'datefield',
								readOnly : true,
								value : new Date()
							}]
						},
						{
							columnWidth : 1,
							layout : 'form',
							border : false,
							items : [menuRoleCombo]
						},
						{
							columnWidth : 1,
							layout : 'form',
							border : false,
							items : [{
								xtype : 'textarea',
								fieldLabel : '备注',
								height : 80,
								id : 'COMMENT',
								name : 'COMMENT',
								anchor : '96%',
								maxLength : 500,
								maxLengthText : '备注输入过长',
								anchor : '99%'
							}]
						}
				
		]});
		
		this.uploadPanel = new Ext.ux.UploadPanel({
			region : 'center',
			title:'附件',
			readOnly : 'view'==this.operType ? true:false,
			orgId : roleId
		});
		
		Ext.apply(this,{
			id : 'attrMgmtWindow',
			title :'上传附件',
			border : false,
	        layout:'border',
	        width : 800,
	        height : 450,
	        modal : true,
			items : [this.form,this.uploadPanel],
			listeners : {
				'render' : this.initData.createDelegate(this)
			}
		});
	},
	initData : function(){
		if('view'==this.operType){
			Ext.getCmp('UPLOAD_OPER_USER_ID').setValue(this.record.get("OPER_USER_ID"));
			Ext.getCmp('UPLOAD_OPER_USER_NAME').setValue(this.record.get("OPER_USER_NAME"));
			Ext.getCmp('UPLOAD_DATE').setValue(this.record.get("OPER_DATE"));
			Ext.getCmp('COMMENT').setValue(this.record.get("COMMENT"));
			this.uploadPanel.load(this.record.get("ID"));
		}
		
	},
	
	onSave : function(){
		var uploadOperUserId = Ext.getCmp('UPLOAD_OPER_USER_ID').getValue();
		var uploadOperUserName = Ext.getCmp('UPLOAD_OPER_USER_NAME').getValue();
		var uploadDate = Ext.getCmp('UPLOAD_DATE').getRawValue();
		var comment = Ext.getCmp('COMMENT').getValue();
		var shareRoleIds = Ext.getCmp('SHARE_ROLE').getValue();
		var shareRoleNames = Ext.getCmp('SHARE_ROLE').getRawValue();
		var atts = this.uploadPanel.getFileIds() + "";
		if(!atts || ''==atts){
			ExtJ.showMessage("对不起,请上传文件!");
			return;
		}
		Ext.getBody().mask("正在保存,请稍候...");
		this.form.getForm().submit({
					method : 'post',
					url : ExtJ.getActionPath("/att/saveAtt.do"),
					params : {
						uploadOperUserId : uploadOperUserId,
						uploadOperUserName : uploadOperUserName,
						uploadDate : uploadDate,
						comment : comment,
						atts : atts,
						roleId : roleId,
						roleName : roleName,
						shareRoleIds : shareRoleIds,
						shareRoleNames : shareRoleNames
					},
					reset : false,
					success : function(option, action){
						var text = Ext.decode(action.response.responseText);
						this.close();
						if(text.success){
							Ext.getCmp('grid').getStore().reload();
							Ext.Msg.alert('提示','保存成功!');
						}else{
							Ext.Msg.alert('提示','保存失败!');
						}
						Ext.getBody().unmask(true);
					}.createDelegate(this)
		});
	},
	
	onCancel : function(){
		this.close();
	}
});